#ifndef REMOTECONTROLLER_H
#define REMOTECONTROLLER_H

#include <QObject>
#include <QPoint>
#include <QSize>
#include <QEvent>
#include <QUdpSocket>
#include <QJsonObject>
#include <QJsonDocument>

class RemoteController : public QObject
{
    Q_OBJECT

public:
    explicit RemoteController(QObject *parent = nullptr);
    ~RemoteController();

    // 核心功能
    bool initialize();
    void setSourceScreenSize(const QSize& size);
    void setTargetScreenSize(const QSize& size);
    void setTargetAddress(const QString& address, quint16 port);

    // 控制开关
    void enable();
    void disable();
    bool isEnabled() const;
    
    // 事件过滤
    bool eventFilter(QObject* obj, QEvent* event) override;
    
    // 公共接口函数
    void sendCommand(const QJsonObject& command);
    QJsonObject createMouseEvent(QEvent::Type type, const QPoint& pos, Qt::MouseButton button = Qt::NoButton);
    QJsonObject createKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers);

signals:
    void enabled();
    void disabled();
    void eventCaptured(const QString& eventInfo);
    void commandSent(const QString& commandInfo);
    void errorOccurred(const QString& error);

private slots:
    void onSocketError(QAbstractSocket::SocketError error);

private:
    void convertCoordinates(QPoint& point);

private:
    // 网络通信
    QUdpSocket* udpSocket;
    QString targetAddress;
    quint16 targetPort;
    
    // 屏幕配置
    QSize sourceScreenSize;
    QSize targetScreenSize;
    
    // 状态
    bool isEnabledFlag;
    
    // 坐标转换
    double scaleX;
    double scaleY;
    
    // 事件统计
    int eventCounter;
};

#endif // REMOTECONTROLLER_H
